﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ooWii.IO
{
	public class GCDirectoryInfo
	{
		#region GCDirectoryInfo

		string name;
		GCDirectoryInfo parent;
		List<GCFileInfo> files = new List<GCFileInfo>();
		List<GCDirectoryInfo> directories = new List<GCDirectoryInfo>();

		public GCDirectoryInfo( string name, GCDirectoryInfo parent )
		{
			this.name = name;
			this.parent = parent;
		}

		public string Name
		{
			get
			{
				return name;
			}
		}

		internal void AddFile( GCFileInfo fileInfo )
		{
			files.Add( fileInfo );
		}

		internal void AddDirectory( GCDirectoryInfo directoryInfo )
		{
			directories.Add( directoryInfo );
		}

		public GCFileInfo[] GetFiles()
		{
			return files.ToArray();
		}

		public GCFileInfo[] GetFiles( string pattern )
		{
			Wildcard wildcard = new Wildcard( pattern );

			List<GCFileInfo> matchingFiles = new List<GCFileInfo>();

			for( int i = 0; i < files.Count; i++ )
			{
				if( wildcard.IsMatch( files[ i ].FullName ) )
				{
					matchingFiles.Add( files[ i ] );
				}
			}

			return matchingFiles.ToArray();
		}

		public GCDirectoryInfo[] GetDirectories()
		{
			return directories.ToArray();
		}

		public GCDirectoryInfo[] GetDirectories( string pattern )
		{
			Wildcard wildcard = new Wildcard( pattern );

			List<GCDirectoryInfo> matchingFiles = new List<GCDirectoryInfo>();

			for( int i = 0; i < directories.Count; i++ )
			{
				if( wildcard.IsMatch( directories[ i ].FullName ) )
				{
					matchingFiles.Add( directories[ i ] );
				}
			}

			return matchingFiles.ToArray();
		}

		public string FullName
		{
			get
			{
				StringBuilder stringBuilder = BuildPath();
				return stringBuilder.ToString();
			}
		}

		internal StringBuilder BuildPath()
		{
			if( parent == null )
			{
				return new StringBuilder();
			}

			StringBuilder stringBuilder = parent.BuildPath();
			stringBuilder.Append( "/" );
			stringBuilder.Append( name );
			return stringBuilder;
		}
		#endregion
	}
}
